<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-20 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		require_once ROOT_PATH.'include/logic/image.logic.php';
		
		$this->excute();
	}
	
	function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'catch':
				$this->catchFromDouban();
				break;
			case 'do_catch':
				$this->doCatchFromDouban();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'hot':
				$this->hot();
				break;
			case 'add_hot':
				$this->addHot();
				break;
			case 'expect':
				$this->expect();
				break;
			case 'add_expect':
				$this->addExpect();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'trash':
				$this->trash();
				break;
			case 'do_recover':
				$this->doRecover();
				break;
			case 'category':
				$this->category();
				break;
			case 'modify_category':
				$this->modifyCategory();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		$page = $this->page_num + 1;
		
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		
		$movie_name = isset($this->request['movie_name']) ? trim($this->request['movie_name']) : '';
		$category_id = isset($this->request['category_id']) ? trim($this->request['category_id']) : '';
		$from_time = isset($this->request['from_time']) ? trim($this->request['from_time']) : '';
		$to_time = isset($this->request['to_time']) ? trim($this->request['to_time']) : '';
		
		//$query_link = "admin.php?m=movie&page={$page}&count={$this->page_limit}";
		$query_link = "admin.php?m=movie";
		$where_list = array();
				
		if($movie_name)
		{
			$where_list['name'] = "(lower(`name`) like lower('%$movie_name%') or lower(`name_en`) like lower('%$movie_name%'))";
			$query_link .= "&movie_name=".urlencode($movie_name);
		}
		if($category_id)
		{
			$where_list['category'] = "category_id = '$category_id'";
			$query_link .= "&category_id=".urlencode($category_id);
		}
		
		if(strtotime($from_time) !== false)
		{
			$time = strtotime($from_time);
			//$where_list['from_time'] = "release_date > '$time'";
			$query_link .= "&from_time=".urlencode($from_time);
		}
		else
		{
			$from_time = date('Y-m-d', time() - 3600*24*30);
		}
		
		if(strtotime($to_time) !== false)
		{
			$time = strtotime($to_time);
			//$where_list['to_time'] = "release_date < '$time'";
			$query_link .= "&to_time=".urlencode($to_time);
		}
		else
		{
			$to_time = date('Y-m-d');
		}
		
		if($type)
		{
			if($type == 'on_show')
			{
				$where_list['type'] = "release_date > '".(time() - 3600*24*7)."' and release_date < '".time()."'";
			}
			else if($type == 'soon')
			{
				$where_list['type'] = "release_date > '".time()."'";
			}
			$query_link .= "&type=".urlencode($type);
		}
		$where_list['delete'] = "is_delete = '0'";
		
		$where = ' where '.implode(' and ', $where_list).' ';
		
		$sql = " select count(1) as `total_record` from `".TABLE_PREFIX."movie` {$where}";
		$total_record = DBFunction::getInstance()->getOne($sql);
		
		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select * from `".TABLE_PREFIX."movie` {$where} order by `release_date` desc limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
						
			foreach($item_list as &$item1)
			{
				$sql = "select count(1) from `".TABLE_PREFIX."movie_review` where movie_id = '{$item1['id']}'";
				$item1['review_count'] = DBFunction::getInstance()->getOne($sql);
			}
		}
		
		$results = get_movie_category_list();
		
		$category_list[] = array('name'=>'全部分类', 'value'=>'0');
		if(check_array($results))
		{
			foreach($results as $item)
			{
				$category_list[] = array('name'=>$item['name'], 'value'=>$item['id']);
			}
		}
		
		$category_select = select('category_id', $category_list, $category_id);
		
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '10 20 50 100 200 500');
		
		include template('admin/movie');
	}
	
	private function add()
	{
		$request = array();
		if(isset($this->request['c']))
		{
			$request = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$request['name'] = isset($request['name']) ? $request['name'] : '';
		$request['director'] = isset($request['director']) ? $request['director'] : '';
		$request['actor'] = isset($request['actor']) ? $request['actor'] : '';
		$request['release_date'] = isset($request['release_date']) ? $request['release_date'] : '';
		
		$request['star'] = isset($request['star']) ? $request['star'] : '';
		$request['category_id'] = isset($request['category_id']) ? $request['category_id'] : '';
		$request['is_hot'] = isset($request['is_hot']) ? 'checked=""' : '';
		$request['is_expect'] = isset($request['is_expect']) ? 'checked=""' : '';
		
		$request['desc'] = isset($request['desc']) ? $request['desc'] : '';
		
		$results = get_movie_category_list();
		
		$category_list = array();
		if(check_array($results))
		{
			foreach($results as $item)
			{
				$category_list[] = array('name'=>$item['name'], 'value'=>$item['id']);
			}
		}
		
		$category_select = select('category_id', $category_list, $request['category_id'], 'style="width:100px;"');
		
		$action = 'do_add';
		$action_name = '影片添加';
		include template('admin/movie_info');
	}
	
	private function catchFromDouban()
	{
		$catch_url = '';
		
		$request['name'] = '';
		$request['director'] = '';
		$request['actor'] = '';
		$request['release_date'] = '';
		
		$request['desc'] = '';
		$request['star'] = '';
		$request['is_hot'] = '';
		$request['is_expect'] = '';
		
		$results = get_movie_category_list();
		
		$category_list = array();
		if(check_array($results))
		{
			foreach($results as $item)
			{
				$category_list[] = array('name'=>$item['name'], 'value'=>$item['id']);
			}
		}
		
		$category_select = select('category_id', $category_list, '', 'style="width:100px;"');
		
		$show_catch = true;
		
		$action = 'do_add';
		$action_name = '影片添加';
		include template('admin/movie_info');
	}
	
	private function doCatchFromDouban()
	{
		$catch_url = isset($this->request['url']) ? $this->request['url'] : '';
		
		if(!$catch_url)
		{
			$this->messager("请指定URL", 'admin.php?m=movie&a=catch');
		}
		
		//error_reporting(E_ALL|E_NOTICE);
		//var_dump(urlencode($catch_url));
		//$content = file_get_contents($catch_url);
		//var_dump($content);
		//exit();
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $catch_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 20);
		//curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
		$content = curl_exec($ch);
		curl_close($ch);
		
		if(!$content)
		{
			$this->messager("不能获取指定URL内容", 'admin.php?m=movie&a=catch');
		}
		
		$content = strip_tags($content);//var_dump($content);
		
		$request['name'] = preg_match("/官方小站:\s*(.+?)\s/", $content, $temp) ? $temp[1] : '';
		$request['director'] = preg_match("/导演:\s*(.+?)\s*编剧/", $content, $temp) ? $temp[1] : '';
		$request['actor'] = preg_match("/主演:\s*(.+?)\s*类型/", $content, $temp) ? $temp[1] : '';
		$request['release_date'] = preg_match("/上映日期:\s*(.+?)\s*片长/", $content, $temp) ? $temp[1] : '';
		
		//$request['desc'] = preg_match("/剧情简介\s*?(.+?)\s*?/", $content, $temp) ? $temp[1] : '';
		$request['desc'] = substr($content, (stripos($content, '(展开全部)') + 17), (stripos($content, '&copy; 豆瓣') - stripos($content, '(展开全部)') - 17));
		//var_dump($request['desc'], stripos($content, '&copy; 豆瓣'));
		unset($temp);
		unset($content);
		
		$request['star'] = '';
		$request['is_hot'] = '';
		$request['is_expect'] = '';
		
		$results = get_movie_category_list();
		
		$category_list = array();
		if(check_array($results))
		{
			foreach($results as $item)
			{
				$category_list[] = array('name'=>$item['name'], 'value'=>$item['id']);
			}
		}
		
		$category_select = select('category_id', $category_list, '', 'style="width:100px;"');
		
		$show_catch = true;
				
		$action = 'do_add';
		$action_name = '影片添加';
		include template('admin/movie_info');
	}
	
	private function doAdd()
	{
		$request = $this->_check_movie_request();
		
		$result = add_movie($request);
		
		if($result)
		{
			$this->messager("添加成功", 'admin.php?m=movie&a=modify&id='.$result);
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function modify()
	{
		$movie_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$movie_id)
		{
			$this->messager("请指定ID", 'admin.php?m=movie');
		}
		
		if(isset($this->request['c']) && ($request = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$request = get_movie_info($movie_id);
			
			if(!isset($request['id']) || !$request['id'])
			{
				$this->messager("影片不存在", 'admin.php?m=movie');
			}
		}
		
		$results = get_movie_category_list();
		
		$category_list = array();
		if(check_array($results))
		{
			foreach($results as $item)
			{
				$category_list[] = array('name'=>$item['name'], 'value'=>$item['id']);
			}
		}
		
		$category_select = select('category_id', $category_list, $request['category_id'], 'style="width:100px;"');
				
		$request['release_date'] = is_numeric($request['release_date']) ? date('Y-m-d H:i:s', $request['release_date']) : $request['release_date'];
		$request['star'] = isset($request['star']) ? $request['star'] : $request['star_level'];
		$request['is_hot'] = ($request['is_hot']) ? 'checked=""' : '';
		$request['is_expect'] = ($request['is_expect']) ? 'checked=""' : '';
				
		if(!isset($request['actor']))
		{
			$actors = get_movie_actor_list($request['id']);
			if(check_array($actors))
			{
				$actor_names = array();
				foreach($actors as $actor)
				{
					$actor_names[] = $actor['actor_name'];
				}
				$request['actor'] = implode(',', $actor_names);
				unset($actors);
				unset($actor_names);
			}
			else
			{
				$request['actor'] = '';
			}
		}
		
		$images = get_movie_image_list($movie_id, '1');
		$image_list = array();
		if(check_array($images))
		{
			foreach($images as $item)
			{
				$image_list[$item['id']]['small'] = 'data/images/movie/'.image_path($item['id']).$item['id'] . '_1.jpg';
				$image_list[$item['id']]['org'] = 'data/images/movie/'.image_path($item['id']).$item['id'] . '_o.jpg';
			}
		}
		$request['movie_face'] = $image_list;
		
		$images = get_movie_image_list($movie_id, '2');
		$image_list = array();
		if(check_array($images))
		{
			foreach($images as $item)
			{
				$image_list[$item['id']]['small'] = 'data/images/movie/'.image_path($item['id']).$item['id'] . '_1.jpg';
				$image_list[$item['id']]['org'] = 'data/images/movie/'.image_path($item['id']).$item['id'] . '_o.jpg';
			}
		}
		$request['image_list'] = $image_list;
		$image_ids = implode(',', array_keys($image_list));
		
		$request['titbit'] = get_movie_titbit_list(array('movie_id'=>$movie_id, 'page_limit'=>100));
		$request['refer'] = get_movie_refer_list(array('movie_id'=>$movie_id, 'page_limit'=>100));
		
		$action = 'do_modify&id='.$request['id'];
		$action_name = '影片编辑';
		include template('admin/movie_info');
	}
	
	private function doModify()
	{
		$request = $this->_check_movie_request();
		
		$result = $this->_update_movie($request);
		
		if($result)
		{
			$this->messager("修改成功", 'admin.php?m=movie');
		}
		else
		{
			$this->messager("修改失败", $redirect);
		}
	}
	
	private function doDelete()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		if(!$ids)
		{
			$this->messager("请指定ID", 'admin.php?m=review');
		}
		
		$trash_mids = get_trash_movie_ids($ids);
		
		if(check_array($trash_mids))
		{
			$mids = array();
			foreach($trash_mids as $mid)
			{
				// delete movie actor
				delete_movie_actors($mid['id']);
				// delete movie titbit
				delete_movie_titbit(array('movie_id'=>$mid['id']));
				//delete movie refer
				delete_movie_refer(array('movie_id'=>$mid['id']));
				
				$mids[] = $mid['id'];
			}
			
			// delete movie description
			delete_movie_desc(implode(',', $mids));
			
			// delete movie images
			$image_ids = get_movie_image_ids(implode(',', $mids));
			if(check_array($image_ids))
			{				
				$ids = array();
				foreach($image_ids as $item)
				{
					$ids[] = $item['id'];
				}
				
				delete_movie_images(implode(',', $ids));
			}
		}
		
		$tempResults = get_movie_review_ids($ids);
		if(check_array($tempResults))
		{
			$review_ids = array();
			foreach($tempResults as $item)
			{				
				$review_ids[] = $item['id'];
			}
			// delete movie review
			delete_movie_review(implode(',', $review_ids));
		}
		
		$result = delete_movie($ids);
		
		if($result)
		{
			$this->messager("删除成功", 'admin.php?m=movie');
		}
		else
		{
			$this->messager("删除失败", 'admin.php?m=movie');
		}
	}
	
	private function hot()
	{
		$page = $this->page_num + 1;
		
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		
		$movie_name = isset($this->request['movie_name']) ? trim($this->request['movie_name']) : '';
		$category_id = isset($this->request['category_id']) ? trim($this->request['category_id']) : '';
		$from_time = isset($this->request['from_time']) ? trim($this->request['from_time']) : '';
		$to_time = isset($this->request['to_time']) ? trim($this->request['to_time']) : '';
		
		$query_link = "admin.php?m=movie&a=hot";
		$where_list = array();
				
		if($movie_name)
		{
			$where_list['name'] = "(lower(`name`) like lower('%$movie_name%') or lower(`name_en`) like lower('%$movie_name%'))";
			$query_link .= "&movie_name=".urlencode($movie_name);
		}
		if($category_id)
		{
			$where_list['category'] = "category_id = '$category_id'";
			$query_link .= "&category_id=".urlencode($category_id);
		}
		
		if(strtotime($from_time) !== false)
		{
			$time = strtotime($from_time);
			//$where_list['from_time'] = "release_date > '$time'";
		}
		else
		{
			$from_time = date('Y-m-d', time() - 3600*24*30);
		}
		
		if(strtotime($to_time) !== false)
		{
			$time = strtotime($to_time);
			//$where_list['to_time'] = "release_date < '$time'";
		}
		else
		{
			$to_time = date('Y-m-d');
		}
		
		if($type == 'hot')
		{
			$where_list['type'] = "is_hot = '1'";
		}
		
		$where = empty($where_list) ? '' : ' where '.implode(' and ', $where_list).' ';
		
		$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."movie` {$where} ";
		$total_record = DBFunction::getInstance()->getOne($sql);
		
		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select * from `".TABLE_PREFIX."movie` {$where} order by `release_date` desc limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
			
			foreach($item_list as &$item1)
			{
				$sql = "select count(1) from `".TABLE_PREFIX."movie_review` where movie_id = '{$item1['id']}'";
				$item1['review_count'] = DBFunction::getInstance()->getOne($sql);
			}
		}
		
		$results = get_movie_category_list();
		
		$category_list[] = array('name'=>'全部分类', 'value'=>'0');
		if(check_array($results))
		{
			foreach($results as $item)
			{
				$category_list[] = array('name'=>$item['name'], 'value'=>$item['id']);
			}
		}
		
		$category_select = select('category_id', $category_list, $category_id);
		
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');
		
		include template('admin/movie_hot');
	}
	
	private function addHot()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		$redirect = 'admin.php?m=movie&a=hot';
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}

		$result = update_movie_status('is_hot', $ids, $type);
		
		if($result)
		{
			$this->messager("操作成功", $redirect);
		}
		else
		{
			$this->messager("操作失败", $redirect);
		}
	}
	
	private function expect()
	{
		$page = $this->page_num + 1;
		
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		
		$movie_name = isset($this->request['movie_name']) ? trim($this->request['movie_name']) : '';
		$category_id = isset($this->request['category_id']) ? trim($this->request['category_id']) : '';
		$from_time = isset($this->request['from_time']) ? trim($this->request['from_time']) : '';
		$to_time = isset($this->request['to_time']) ? trim($this->request['to_time']) : '';
		
		$query_link = 'admin.php?m=movie&a=expect';
		$where_list = array();
				
		if($movie_name)
		{
			$where_list['name'] = "(lower(`name`) like lower('%$movie_name%') or lower(`name_en`) like lower('%$movie_name%'))";
			$query_link .= "&movie_name=".urlencode($movie_name);
		}
		if($category_id)
		{
			$where_list['category'] = "category_id = '$category_id'";
			$query_link .= "&category_id=".urlencode($category_id);
		}
		
		if(strtotime($from_time) !== false)
		{
			$time = strtotime($from_time);
			//$where_list['from_time'] = "release_date > '$time'";
		}
		else
		{
			$from_time = date('Y-m-d', time() - 3600*24*30);
		}
		
		if(strtotime($to_time) !== false)
		{
			$time = strtotime($to_time);
			//$where_list['to_time'] = "release_date < '$time'";
		}
		else
		{
			$to_time = date('Y-m-d');
		}
		
		if($type == 'expect')
		{
			$where_list['type'] = "is_expect = '1'";
		}
		
		$where = empty($where_list) ? '' : ' where '.implode(' and ', $where_list).' ';
		
		$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."movie` {$where} ";
		$total_record = DBFunction::getInstance()->getOne($sql);
		
		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select * from `".TABLE_PREFIX."movie` {$where} order by `release_date` desc limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
			
			foreach($item_list as &$item1)
			{
				$sql = "select count(1) from `".TABLE_PREFIX."movie_review` where movie_id = '{$item1['id']}'";
				$item1['review_count'] = DBFunction::getInstance()->getOne($sql);
			}
		}
		
		$results = get_movie_category_list();
		
		$category_list[] = array('name'=>'全部分类', 'value'=>'0');
		if(check_array($results))
		{
			foreach($results as $item)
			{
				$category_list[] = array('name'=>$item['name'], 'value'=>$item['id']);
			}
		}
		
		$category_select = select('category_id', $category_list, $category_id);
		
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');
		
		include template('admin/movie_expect');
	}
	
	private function addExpect()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		$redirect = 'admin.php?m=movie&a=expect';
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}

		$result = update_movie_status('is_expect', $ids, $type);
		
		if($result)
		{
			$this->messager("操作成功", $redirect);
		}
		else
		{
			$this->messager("操作失败", $redirect);
		}
	}
	
	private function trash()
	{
		$page = $this->page_num + 1;
		
		$query_link = 'admin.php?m=movie&a=trash';
		$where = "where is_delete = '1'";
		
		$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."movie` {$where} ";
		$total_record = DBFunction::getInstance()->getOne($sql);
		
		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select * from `".TABLE_PREFIX."movie` {$where} order by `release_date` desc limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
		}
		
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');
		
		include template('admin/movie_trash');
	}
	
	private function doRecover()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		$redirect = 'admin.php?m=movie&a=trash';
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}

		$result = update_movie_status('is_delete', $ids, 'recover');
		
		if($result)
		{
			$this->messager("操作成功", $redirect);
		}
		else
		{
			$this->messager("操作失败", $redirect);
		}
	}
	
	private function _check_movie_request()
	{
		$request['name'] = isset($this->request['name']) ? trim($this->request['name']) : '';
		$request['director'] = isset($this->request['director']) ? trim($this->request['director']) : '';
		$request['actor'] = isset($this->request['actor']) ? trim($this->request['actor']) : '';
		$request['release_date'] = isset($this->request['release_date']) ? trim($this->request['release_date']) : '';
		
		$request['star'] = isset($this->request['star']) ? (int)$this->request['star'] : 0;
		$request['category_id'] = isset($this->request['category_id']) ? (int)$this->request['category_id'] : '';
		$request['is_hot'] = isset($this->request['is_hot']) ? true : false;
		$request['is_expect'] = isset($this->request['is_expect']) ? true : false;
		//$request['mark'] = isset($this->request['mark']) ? $this->request['mark'] : '';
		
		$request['desc'] = isset($this->request['desc']) ? trim($this->request['desc']) : '';
				
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "admin.php?m=movie&a=add&c=$key";
				
		$message = '';
		if(!$request['name'] || !$request['director'] || !$request['actor'] || !$request['release_date'] || !$request['desc'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($request['name']) > 90)
		{
			$this->messager("影片名称超长", $redirect);
		}
		if(strlen($request['director']) > 90)
		{
			$this->messager("导演姓名超长", $redirect);
		}
		if(strlen($request['actor']) > 250)
		{
			$this->messager("演员姓名超长", $redirect);
		}
		if(strlen($request['desc']) > 3000)
		{
			$this->messager("影片简介超长", $redirect);
		}
		if(!strtotime($request['release_date']))
		{
			$this->messager("上映时间格式错误", $redirect);
		}
		
		$request['id'] = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$is_exists = check_movie_exists($request['name'], $request['id']);
		if($is_exists)
		{
			$this->messager("影片名称 《{$request['name']}》 已存在", 'admin.php?m=movie');
		}
		
		$request['release_date'] = strtotime($request['release_date']);
		$request['is_hot'] = $request['is_hot'] ? 1 : 0;
		$request['is_expect'] = $request['is_expect'] ? 1 : 0;
		//$request['is_hot'] = (isset($request['mark']) && $request['mark'] == 'is_hot') ? 1 : 0;
		//$request['is_expect'] = (isset($request['mark']) && $request['mark'] == 'is_expect') ? 1 : 0;
		$request['actor'] = explode(',', $request['actor']);
				
		return $request;
	}
	
	private function _update_movie($request)
	{
		$request['id'] = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$request['id'])
		{
			$this->messager("请指定ID", 'admin.php?m=movie');
		}

		$movie = get_movie_info($request['id']);
			
		if(!isset($movie['id']) || !$movie['id'])
		{
			$this->messager("影片不存在", 'admin.php?m=movie');
		}
		
		// check if need update actors
		$actors = get_movie_actor_list($request['id']);
		if(check_array($actors))
		{
			$actor_names = array();
			foreach($actors as $actor)
			{
				$actor_names[] = $actor['actor_name'];
			}
			if(md5($this->request['actor']) == md5(implode(',', $actor_names)))
			{
				$request['actor'] = array();
			}
		}
		
		$result = update_movie($request);
				
		return $result;
	}
	
	private function category()
	{
		$category_list = get_movie_category_list();
		include template('admin/movie_category');
	}
	
	private function modifyCategory()
	{
		$list = isset($this->request['category']) ? $this->request['category'] : array();
		$category_new = isset($this->request['category_new']) ? $this->request['category_new'] : array();
		
		$redirect = 'admin.php?m=movie&a=category';
		
		$category_list = get_movie_category_list();
		
		foreach($category_list as $k=>&$item)
		{
			if(isset($list[$k]))
			{
				if($item['name'] != trim($list[$k]['name']) || $item['order'] != trim($list[$k]['order']))
				{
					$item['name'] = trim($list[$k]['name']);
					$item['order'] = (int)$list[$k]['order'];
					
					update_movie_category($item);
				}
			}
		}
		unset($list);
		unset($category_list);
		
		if(check_array($category_new))
		{
			foreach($category_new as &$item)
			{
				$item['name'] = trim($item['name']);
				$item['order'] = (int)$item['order'];
				if($item['name'] != '' && $item['order'] > 0)
				{
					add_movie_category($item);
				}
			}
		}
		unset($category_new);
		
		$this->messager('', $redirect, 0);
	}
}